import 'dart:async';
import 'dart:convert';

import 'package:flutter/services.dart';

import 'base_response.dart';

class NetworkPlugin {
  static const MethodChannel _channel = const MethodChannel('network_plugin');

  static Future<String> rawGet(String path, {Map params}) async {
    return await _channel
        .invokeMethod("rawGet", {"path": path, "params": params});
  }

  static Future<BaseResponse> get(String path, {Map params}) async {
    try {
      return BaseResponse.fromJson(
          jsonDecode(await rawGet(path, params: params)));
    } catch (e, s) {
      print(s);
      return null;
    }
  }

  static Future<String> rawPost(String path, {Map params}) async {
    return await _channel
        .invokeMethod("rawPost", {"path": path, "params": params});
  }

  static Future<BaseResponse> post(String path, {Map params}) async {
    try {
      return BaseResponse.fromJson(
          jsonDecode(await rawPost(path, params: params)));
    } catch (e, s) {
      print(s);
      return null;
    }
  }
}
